Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Globalization

Public Partial Class FrmPedido
	Inherits Form
	Private precios As Double = 0
    Private total As Double = 0
    Dim daoPedi As New DAOPedido

	Public Sub New()
		InitializeComponent()
		label10.Text = DateTime.Now.ToShortDateString()
		'fecha del sistema

		radioButton1.Checked = True
	End Sub

    Private Sub cmd_guardar_Click(ByVal sender As Object, ByVal e As EventArgs)
        'boton que al guardar pregunta si quiere imprimir un recibo
        If radioButton2.Checked = True Then
            MessageBox.Show("aca se imprimiria la factura")
        Else
            If MessageBox.Show("¿Desea Imprimir el remito?", "Pedido", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Else
                limpiarForm()
            End If
        End If

    End Sub

    Private Sub txtCant_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        'evento para poner solo nros y coma
        If [Char].IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf [Char].IsControl(e.KeyChar) Then
            e.Handled = False
        ElseIf [Char].IsSeparator(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
        Dim cc As CultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture
        If Char.IsNumber(e.KeyChar) OrElse e.KeyChar.ToString() = cc.NumberFormat.NumberDecimalSeparator Then
            e.Handled = False
        End If

    End Sub

    Private Sub btnAgregar_Click(ByVal sender As Object, ByVal e As EventArgs)
        If txtCant.Text <> "" Then
            Dim subtotal As Double = 0
            subtotal = precios * Double.Parse(txtCant.Text)
            total += subtotal
            dgvDetalle.Rows.Add(txtNroProd.Text, txtProd.Text, txtUnidMed.Text, txtCant.Text, subtotal)
            lblTotal.Text = total.ToString()
            limpiarPedido()
        Else
            MessageBox.Show("Debe ingresar la cantidad")
        End If

    End Sub

	Private Sub limpiarPedido()
	'limpia los datos del pedido al presionar agregar
		txtNroProd.Text = ""
		txtProd.Text = ""
		txtUnidMed.Text = ""
		txtCant.Text = ""
		txtNroProd.Focus()
	End Sub

	'metodo de autocompletar para buscar el cliente al ir tipeando en el textbox de razon soc o codig
    Private Sub FrmPedido_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim cli As New DAOCliente
        Dim prod As New DAOProducto
        lblTotal.Text = "" 'setea el valor del laber sin valor
        txtRazonSocial.AutoCompleteCustomSource.Clear()
        'limpio cualquier cosa q tenga
        Dim dt As SqlDataReader = cli.DAOcargarTextBoxCliente()
        'traigo el datareader con el rtdo de la consulta
        txtProd.AutoCompleteCustomSource.Clear()
        Dim dt1 As SqlDataReader = prod.DAOcargarTextBoxProducto()

        While dt.Read()
            'recorro
            'agrego los items
            txtRazonSocial.AutoCompleteCustomSource.Add(dt(1).ToString())
        End While

        While dt1.Read()
            txtProd.AutoCompleteCustomSource.Add(dt1(1).ToString())
        End While
    End Sub

    Private Sub txtCodClien_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        'evento para q buaque al presionar enter en el txtCodCli
        If [Char].IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf [Char].IsControl(e.KeyChar) Then
            e.Handled = False
        ElseIf [Char].IsSeparator(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Enter) Then
            cargarGrillaObras()
            cargarNombreCliente()
        End If
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        'para agregar un nuevo cliente xq no está en la lista
        Dim cli As New FrmClient(False)
        cli.ShowDialog()
    End Sub

    Private Sub radioButton2_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        'desactivo los campos del cliente
        groupBox2.Enabled = False
        txtCodClien.Enabled = False
        txtRazonSocial.Enabled = False
        btnBuscarCli.Enabled = False
        dateTimePicker1.Enabled = False
    End Sub

    Private Sub radioButton1_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        'activo los campos del cliente
        groupBox2.Enabled = True
        txtCodClien.Enabled = True
        txtRazonSocial.Enabled = True
        btnBuscarCli.Enabled = True
        dateTimePicker1.Enabled = True
    End Sub

	Private Sub cargarGrillaObras()
        'carga la grilla de obras
        Dim daoObras As New DAOObras
		Dim idcliente As Integer = Integer.Parse(txtCodClien.Text)
        dgvObras.DataSource = daoObras.cargarGrillaObras(idcliente)
	End Sub

    Private Sub cargarNombreCliente() 'carga el nombre del cliente
        Dim cli As New DAOCliente
        Dim nombre As String = ""
        Dim nroCli As Integer = Integer.Parse(txtCodClien.Text)
        nombre = cli.buscarNombreCliente(nroCli)

        If nombre = "" Then
            MessageBox.Show("No existe cliente con ese numero de cliente")
            txtRazonSocial.Text = ""
            txtCodClien.Text = ""
            txtCodClien.Focus()
        Else
            txtRazonSocial.Text = nombre
        End If
    End Sub

	Private Sub cargarNombreProducto()
	'cargo el nombre y um cuando cargo el cod prod
		Dim nombreProd As String = ""
		Dim nombreUM As String = ""
		Dim codPro As String = txtNroProd.Text
        daoPedi.buscarNombreProducto(codPro, nombreProd, nombreUM, precios)
		'ref para retornar dos valores
		If nombreProd = "" Then
			MessageBox.Show("No existe ese codigo de producto")
			txtRazonSocial.Text = ""
			txtCodClien.Text = ""
			txtCodClien.Focus()
		Else
			txtProd.Text = nombreProd
			txtUnidMed.Text = nombreUM
		End If
	End Sub

    Private Sub txtCodClien_Validated(ByVal sender As Object, ByVal e As EventArgs)
        'evento al quitar el foco del txt codCliente
        If txtCodClien.Text <> "" Then
            cargarGrillaObras()
            cargarNombreCliente()
        End If
    End Sub

    Private Sub txtRazonSocial_Validated(ByVal sender As Object, ByVal e As EventArgs)
        'evento al quitar el foco del txtRazon social
        Dim cli As New DAOCliente
        If Me.txtRazonSocial.Text <> "" Then
            txtCodClien.Text = cli.buscarIdCliente(txtRazonSocial.Text)
            'primero cargo el codigo del producto
            cargarGrillaObras()
        End If
    End Sub

    Private Sub txtNroProd_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If e.KeyChar = Convert.ToChar(Keys.Enter) Then
            'evento al precionar enter
            cargarNombreProducto()
        End If
    End Sub

    Private Sub btnNuevo_Click(ByVal sender As Object, ByVal e As EventArgs)
        limpiarForm()
    End Sub

	Private Sub limpiarForm()
		txtCant.Text = ""
		radioButton1.Checked = True
		txtCodClien.Text = ""
		txtRazonSocial.Text = ""
		dgvObras.DataSource = ""
		dgvDetalle.Rows.Clear()
		txtNroProd.Text = ""
		txtProd.Text = ""
		txtUnidMed.Text = ""
		precios = 0
		total = 0
		lblTotal.Text = ""
	End Sub

    Private Sub btnCancelar_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Close()
    End Sub

	'evento q al hacer click sobre la grilla de productos las va eliminando
    Private Sub dgvDetalle_CellContentClick(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        total = total - (Double.Parse(dgvDetalle.CurrentRow.Cells("subtotal").Value.ToString()))
        lblTotal.Text = total.ToString()
        dgvDetalle.Rows.Remove(dgvDetalle.CurrentRow)
        'elimino la fila (solo de la grilla, no de la BD) del detalle al hacer click
    End Sub

    

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        cargarNombreProducto()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscarCli.Click

    End Sub

   
End Class
